package com.auxgroup.smarthome.entity;

import com.auxgroup.smarthome.session.SessionUser;
import com.auxgroup.smarthome.utils.EncryptionUtils;
import com.auxgroup.smarthome.utils.NewDateUtils;
import com.fasterxml.jackson.annotation.JsonIgnore;
import org.hibernate.annotations.GenericGenerator;

import javax.persistence.*;
import javax.persistence.Entity;

/**
 * Created by kevinchen on 2016/12/20.
 */
@Entity
@Table(name = "sys_user", indexes = {@Index(columnList = "username", unique = true, name = "index_user_username")})
@Inheritance(strategy = InheritanceType.JOINED)
public class UserEntity extends AbstractGenericEntity {

    @Id
    @GeneratedValue(generator = "uuid2")
    @GenericGenerator(name = "uuid2", strategy = "uuid2")
    @Column(length = 36)
    private String id;

    @Column(length = 100, nullable = false, name = "username",unique = true)
    private String userName;//用于登陆的用户名

    @JsonIgnore //序列化的时候忽略 密码属性
    @Column(length = 255, nullable = false)
    private String password;

    @Column(length = 100)
    private String nickName;//昵称或用户姓名

    @Column(length = 50)
    private String realName;

    @Column(name = "email", length = 100, unique = true)
    private String email;

    @Column(name = "phone", length = 20)
    private String phone;

    @Enumerated(EnumType.ORDINAL)
    @Column(length = 10)
    private UserStatus userStatus = UserStatus.NORMAL;

    @Enumerated(EnumType.ORDINAL)
    @Column(length = 10)
    private LoginStatus loginStatus = LoginStatus.OFF;

    @Enumerated(EnumType.ORDINAL)
    @Column(length = 10)
    private UserTypeEnmu userType = UserTypeEnmu.BACKSTAGE;

    @Column(columnDefinition = "text")
    private String headImg;  // 头像

    /**
     * 上一次登录时间
     */
    private Long lastLoginTime;

    @JsonIgnore
    @ManyToOne(fetch = FetchType.EAGER)
    private RoleEntity role;

    /**
     * 登录失败次数：
     * 1、登录失败5次后需要输入验证码才能登录
     * 2、登录失败10次后停用该账号，需超级管理员恢复
     * 3、超级管理员登录失败10次后禁止登录2个小时
     */
    @Column
    private Integer loginFailCounter = 0;

    private Long loginFailTime;

    /**用于加密*/
    private String salt;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getHeadImg() {
        return headImg;
    }

    public void setHeadImg(String headImg) {
        this.headImg = headImg;
    }

    public LoginStatus getLoginStatus() {
        return loginStatus;
    }

    public void setLoginStatus(LoginStatus loginStatus) {
        this.loginStatus = loginStatus;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public UserStatus getUserStatus() {
        return userStatus;
    }

    public void setUserStatus(UserStatus userStatus) {
        this.userStatus = userStatus;
    }

    public RoleEntity getRole() {
        return role;
    }

    public void setRole(RoleEntity role) {
        this.role = role;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getNickName() {
        return nickName;
    }

    public void setNickName(String nickName) {
        this.nickName = nickName;
    }

    public String getRoleName(){
        return role !=null ? role.getRoleName() : "";
    }

    public String getSalt() {
        return salt;
    }

    public void setSalt(String salt) {
        this.salt = salt;
    }

    public UserTypeEnmu getUserType() {
        return userType;
    }

    public void setUserType(UserTypeEnmu userType) {
        this.userType = userType;
    }

    public String getRealName() {
        return realName;
    }

    public void setRealName(String realName) {
        this.realName = realName;
    }

    public Long getLastLoginTime() {
        return lastLoginTime;
    }

    public void setLastLoginTime(Long lastLoginTime) {
        this.lastLoginTime = lastLoginTime;
    }

    public Integer getLoginFailCounter() {
        return loginFailCounter;
    }

    public void setLoginFailCounter(Integer loginFailCounter) {
        this.loginFailCounter = loginFailCounter;
    }

    public Long getLoginFailTime() {
        return loginFailTime;
    }

    public void setLoginFailTime(Long loginFailTime) {
        this.loginFailTime = loginFailTime;
    }

    public UserEntity() {
    }

    public UserEntity(String userName, String realName, String phone, RoleEntity role) {
        this.userName = userName;
        this.realName = realName;
        this.phone = phone;
        this.role = role;
    }

    //用户身份认证
    public boolean isAuthentication(String pwd){
       return  password.equals(EncryptionUtils.md5(pwd+salt)) ? true : false;
    }

    public SessionUser createSessionUser(){
        return new SessionUser(id, userName, nickName, phone, email,userType, headImg, role.createSessionRole());
    }

    public void modifyPassword(String newPass){
        this.password = EncryptionUtils.md5(newPass+salt);
    }

    public void recordLoginFail() {
        this.loginFailCounter = this.loginFailCounter+1;
        this.loginFailTime = NewDateUtils.getCurrentEpochSecond();
        if(this.loginFailCounter >= 10) {
            this.userStatus = UserStatus.DISABLED;
        }
    }

}
